
//声明一个函数对象 eventHub
var EventHub = function () {
    // 订阅列表
    this.calls = {};
}
/**
 *   利用 js 自带的call()函数
 *   通知订阅的人们
 **/
EventHub.prototype.each = function (obj, fn) {
    var _this = this, key;
    if (typeof fn !== 'function') return _this;
    obj = obj || [];
    if (obj.constructor === Object) {
        for (key in obj) {
            if (fn.call(obj[key], key, obj[key])) break;
        }
    } else {
        for (key = 0; key < obj.length; key++) {
            if (fn.call(obj[key], key, obj[key])) break;
        }
    }
    return _this;
}


// 订阅方法
EventHub.prototype.on = function (channel, callback) {
    if (typeof callback === 'function') {
        this.calls[channel] ? this.calls[channel].push(callback) : this.calls[channel] = [callback];
    }
    return this;
}


//通知方法
EventHub.prototype.emit = function (channel, data) {
    this.each(this.calls[channel], function (index, item) {
        item && item(data);
    })
}


/**
 *  这是一个跟业务相关的额外扩展
 */

// EventHub.prototype.send = function ( data) {
//     this.emit('send',data);
// }




var eventHub = new EventHub();





class socket  {
    // 地址
    address = "";
    // socket对象
    socket;
    // 重连锁
    lockRecon = false

    constructor(host,port) {
        this.init(host, port);
    }

    on(event,data){
        eventHub.on(event ,data)
    }
    emit(event,data){
        eventHub.emit(event,data)
    }



    init(host,port) {
        this.address = "ws://" + host + ":" + port + "/";
        this.connect();
    }

    connect() {
        if (!this.socket) {
            this.socket = new WebSocket(this.address);
            //设置二进制为 arraybuffer
            this.socket.binaryType = "arraybuffer";
            this.socket.onmessage = this.getMessage;
            this.socket.onopen = this.open;
            this.socket.onclose = this.close
            this.socket.onerror = this.error;
        }
    }

    active() {
        return (this.socket !== undefined && this.socket.readyState === WebSocket.OPEN)
    }

    // 重连 机制自行补充
    reconnect() {
        if (!this.active()) {
            if (this.lockRecon) return
            this.lockRecon = true
            this.connect();
            setTimeout(() => {
                this.lockRecon = false
                this.reconnect();
            }, 2000);
        }
    }

    // 销毁
     disconnect() {
        if (!this.socket) {
            return
        }
        if (this.socket.readyState !== WebSocket.CLOSED) {
            this.socket.close()
        }
    }

    getMessage(event) {
        eventHub.emit("onMessage", data);
    }

    send(data) {
        this.socket.send(data);
    }

    open(event) {

        console.log("websocket 连接开启")
        eventHub.emit("open", event)
    }

    close(event) {
        eventHub.emit("websocket", event)
    }

    error(event) {
        eventHub.emit("websocket", event)
    }


}

export default socket